package com.demo;

import java.io.File;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**多个MUST的Query的文档号合并
 * @author Ziroom
 *
 */
public class DemoMust {

	private static final String PATH = "d:\\zsl\\index6";

	public static void createIndex() throws Exception {
        Directory directory = FSDirectory.open(new File(PATH).toPath());
        IndexWriterConfig config = new IndexWriterConfig(new WhitespaceAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, config);
        // 0
	    Document doc = new Document();
	    doc.add(new TextField("content", "a", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 1
	    doc = new Document();
	    doc.add(new TextField("content", "b", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 2
	    doc = new Document();
	    doc.add(new TextField("content", "c b", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 3
	    doc = new Document();
	    doc.add(new TextField("content", "a c", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 4
	    doc = new Document();
	    doc.add(new TextField("content", "h", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 5
	    doc = new Document();
	    doc.add(new TextField("content", "c e", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 6
	    doc = new Document();
	    doc.add(new TextField("content", "c a", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 7
	    doc = new Document();
	    doc.add(new TextField("content", "f e", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 8
	    doc = new Document();
	    doc.add(new TextField("content", "a c d e c e", Field.Store.YES));
	    indexWriter.addDocument(doc);
	    // 9
	    doc = new Document();
	    doc.add(new TextField("content", "a c e a b c", Field.Store.YES));
	    indexWriter.addDocument(doc);
        indexWriter.close();
    }

	public static void searchIndex() throws Exception {
        Directory directory = FSDirectory.open(new File(PATH).toPath());
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new TermQuery(new Term("content", "a")), BooleanClause.Occur.MUST);
        builder.add(new TermQuery(new Term("content", "b")), BooleanClause.Occur.MUST);
        builder.add(new TermQuery(new Term("content", "c")), BooleanClause.Occur.MUST);
        builder.add(new TermQuery(new Term("content", "e")), BooleanClause.Occur.MUST);
        BooleanQuery query = builder.build();
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("查询总记录数：" + topDocs.totalHits);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc doc : scoreDocs) {
            int docId = doc.doc;
            Document document = indexSearcher.doc(docId);
            System.out.println(docId);
            System.out.println(document.get("content"));

        }
        indexReader.close();
    }

	public static void main(String[] args) throws Exception {
		createIndex();
		searchIndex();
	}

}
